package com.ipzoe.file.config;

import com.alibaba.alicloud.oss.OssApplicationListener;
import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.comm.Protocol;
import com.ipzoe.file.config.properties.FileServerProperties;
import com.ipzoe.file.domain.FileInfoDO;
import com.ipzoe.file.service.AbstractFileDOService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author Eric
 * @date 2020/6/12
 */
@Configuration
@EnableConfigurationProperties(FileServerProperties.class)
@ConditionalOnProperty(prefix = "ruoyi.file-server",name = "type", havingValue = "aliyun")
public class AliyunOSSAutoConfigure {

    @Autowired
    private FileServerProperties fileServerProperties;

    @Bean
    public OSSClient ossClient(FileServerProperties fileServerProperties){

        ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
        // 设置是否支持将自定义域名作为Endpoint，默认支持。
        conf.setSupportCname(true);
        // 设置是否开启二级域名的访问方式，默认不开启。
        conf.setSLDEnabled(false);
        // 设置连接OSS所使用的协议（HTTP/HTTPS），默认为HTTP。
        conf.setProtocol(Protocol.HTTP);

        return (OSSClient) new OSSClientBuilder().build(fileServerProperties.getOss().getEndPoint(), fileServerProperties.getOss().getAk(), fileServerProperties.getOss().getSk(), conf);

    }

    @Service
    public class AliyunOssDOServiceImpl extends AbstractFileDOService {

        @Autowired
        private OSSClient ossClient;

        /**
         * 文件来源
         *
         * @return
         */
        @Override
        protected String fileType() {
            return fileServerProperties.getType();
        }

        /**
         * 上传文件
         *
         * @param file
         * @param fileInfo
         * @author Eric
         * @date 2020/3/25
         */
        @Override
        protected void uploadFile(MultipartFile file, FileInfoDO fileInfo) throws IOException {

            ossClient.putObject(fileServerProperties.getOss().getBucketName(), fileInfo.getPath(), file.getInputStream());
            fileInfo.setUrl(fileServerProperties.getOss().getDomain() + File.separator + fileInfo.getPath());
        }

        /**
         * 删除文件资源
         *
         * @param fileInfo
         * @return
         */
        @Override
        protected boolean deleteFile(FileInfoDO fileInfo) {
            ossClient.deleteObject(fileServerProperties.getOss().getBucketName(), fileInfo.getName());
            return true;
        }
    }
}